package com.ryhx.pay.base.app.userinfo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.ryhx.pay.base.app.modelUserInfo;
import com.ryhx.pay.base.app.userinfo.dao.UserInfoDao;
import com.ryhx.pay.base.app.userinfo.service.UserInfoService;
import com.ryhx.pay.common.exceptions.BizException;
import com.ryhx.pay.common.utils.BeanToMapUtils;

@Component("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
	@Resource(name = "userInfoDao")
	private UserInfoDao userInfoDao;

	@Override
	public UserInfo getUserInfo(Long entityId) throws BizException {
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("entityId", entityId);
		return userInfoDao.getUserInfo(query);
	}

	@Override
	public UserInfo selectOneUserInfoWillThrowException(Map<String, Object> query) throws BizException{
		return userInfoDao.getUserInfo(query);
	}

	@Override
	public UserInfo selectFirstUserInfo(Map<String, Object> query) throws BizException{
		query.put("limit", 1);
		List<UserInfo> list = userInfoDao.getUserInfoList(query);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<UserInfo> getUserInfoList(Map<String, Object> query) throws BizException {
		UserInfo userInfo = (UserInfo) query.get("userInfo");
		if (null != userInfo) {
			Class<?> clazz = userInfo.getClass();
			BeanToMapUtils.putBeanFiled(query, userInfo, clazz);
		}
		return userInfoDao.getUserInfoList(query);
	}

	@Override
	public Integer getUserInfoListCount(Map<String, Object> query) throws BizException {
		UserInfo userInfo = (UserInfo) query.get("userInfo");
		if (null != userInfo) {
			Class<?> clazz = userInfo.getClass();
			BeanToMapUtils.putBeanFiled(query, userInfo, clazz);
		}
		return userInfoDao.getUserInfoListCount(query);
	}

	@Override
	public Long insertUserInfo(UserInfo userInfo) throws BizException {
		return userInfoDao.insertUserInfo(userInfo);
	}

	@Override
	public Long replaceUserInfo(UserInfo userInfo) throws BizException {
		return userInfoDao.replaceUserInfo(userInfo);
	}

	@Override
	public int updateUserInfo(UserInfo userInfo) throws BizException {
		return userInfoDao.updateUserInfo(userInfo);
	}

	@Override
	public int updateUserInfoWithoutNull(UserInfo userInfo) throws BizException {
		return userInfoDao.updateUserInfoWithoutNull(userInfo);
	}

	@Override
	public int updateUserInfoByWhereSql(Map<String, Object> userInfo, String whereSql) throws BizException {
		if (userInfo == null || userInfo.isEmpty()) {
			throw new RuntimeException("userInfo不能为空");
		}
		if (StringUtils.isBlank(whereSql)) {
			throw new RuntimeException("whereSql不能为空");
		}
		userInfo.put("whereSql", whereSql);
		return userInfoDao.updateUserInfoByWhereSql(userInfo);
	}

	@Override
	public int deleteUserInfo(Long entityId) throws BizException {
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("entityId", entityId);
		return userInfoDao.deleteUserInfo(query);
	}

	@Override
	public int deleteUserInfoList(Map<String, Object> userInfo) throws BizException{
		return userInfoDao.deleteUserInfoList(userInfo);
	}

	@Override
	public int deleteUserInfoByWhereSql(String whereSql) throws BizException {
		if (StringUtils.isBlank(whereSql)) {
			throw new RuntimeException("whereSql不能为空");
		}
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("whereSql", whereSql);
		return userInfoDao.deleteUserInfoByWhereSql(query);
	}

	@Override
	public int truncateUserInfo() throws BizException {
		return userInfoDao.truncateUserInfo();
	}

	@Override
	public int batchInsertUserInfo(List<UserInfo> userInfoList) throws BizException {
		return userInfoDao.batchInsertUserInfo(userInfoList);
	}

	@Override
	public int batchReplaceUserInfo(List<UserInfo> userInfoList) throws BizException {
		return userInfoDao.batchReplaceUserInfo(userInfoList);
	}

	@Override
	public int batchUpdateUserInfo(List<UserInfo> userInfoList) throws BizException {
		return userInfoDao.batchUpdateUserInfo(userInfoList);
	}

	@Override
	public int batchDeleteUserInfo(List<Long> idList) throws BizException {
		return userInfoDao.batchDeleteUserInfo(idList);
	}

	@Override
	public int batchDeleteUserInfoList(List<UserInfo> userInfoList) throws BizException{
		return userInfoDao.batchDeleteUserInfoList(userInfoList);
	}
}
